package com.example.demo.shops.controller;

import com.example.demo.admin.bean.AdminBean;
import com.example.demo.controller.BaseController;
import com.example.demo.service.BaseService;
import com.example.demo.shops.bean.ShopBean;
import com.example.demo.shops.service.ShopService;
import com.example.demo.sicktype.service.SickTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("Admin/shops")
public class ShopController extends BaseController<ShopService,ShopBean> {

    @Autowired
    SickTypeService sickTypeService;

    public ShopController() {
        this.rootName = "/shops/";
    }



    @Override
    @RequestMapping(value = "save",method = RequestMethod.GET)
    public String saveEntity(ShopBean entity, BindingResult result) {
        return rootName+"add";
    }


    @Override
    public String addNewEntity(Model model) {
        model.addAttribute("typelist",sickTypeService.getAll());
        return super.addNewEntity(model);
    }

    @Override
    public String editEntity(@PathVariable  int id, Model model) {
        model.addAttribute("typelist",sickTypeService.getAll());
        return super.editEntity(id, model);
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    public String saveEntity(ShopBean entity, BindingResult result,String[] bannerimg) {
        if (result.hasErrors())
        {
            return rootName+"add";
        }
        service.save(entity,bannerimg);
        return "redirect:"+DefRequestName+rootName+"list";
    }
}
